{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "检验结果:\n",
      "样本数量: 357\n",
      "原始均值: -90468.3390\n",
      "原始标准差: 157039111.5548\n",
      "K-S统计量: 0.1550\n",
      "P值: 0.0000\n",
      "结论（α=0.05）: 数据不符合正态分布（拒绝原假设）\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from scipy.stats import kstest\n",
    "import argparse\n",
    "\n",
    "def cal(input_file,column):\n",
    "\n",
    "    # 读取CSV文件\n",
    "    try:\n",
    "        df = pd.read_csv(input_file,encoding='GB2312')\n",
    "    except FileNotFoundError:\n",
    "        print(f\"错误：文件 {input_file} 未找到\")\n",
    "        return\n",
    "\n",
    "    # 检查列是否存在\n",
    "    if column not in df.columns:\n",
    "        print(f\"错误：列 '{column}' 不存在于文件中\")\n",
    "        return\n",
    "\n",
    "    # 提取数据并移除缺失值\n",
    "    data = df[column].dropna().values\n",
    "    if len(data) < 2:\n",
    "        print(\"错误：数据量不足（需要至少2个有效数据点）\")\n",
    "        return\n",
    "\n",
    "    # 数据标准化（Z-score标准化）\n",
    "    mean = np.mean(data)\n",
    "    std = np.std(data, ddof=1)\n",
    "    normalized_data = (data - mean) / std\n",
    "\n",
    "    # 执行K-S检验（与标准正态分布比较）\n",
    "    ks_statistic, p_value = kstest(normalized_data, 'norm')\n",
    "\n",
    "    # 输出结果\n",
    "    print(\"\\n检验结果:\")\n",
    "    print(f\"样本数量: {len(data)}\")\n",
    "    print(f\"原始均值: {mean:.4f}\")\n",
    "    print(f\"原始标准差: {std:.4f}\")\n",
    "    print(f\"K-S统计量: {ks_statistic:.4f}\")\n",
    "    print(f\"P值: {p_value:.4f}\")\n",
    "\n",
    "    # 显著性判断（α=0.05）\n",
    "    alpha = 0.05\n",
    "    if p_value < alpha:\n",
    "        print(f\"结论（α={alpha}）: 数据不符合正态分布（拒绝原假设）\")\n",
    "    else:\n",
    "        print(f\"结论（α={alpha}）: 数据符合正态分布（未拒绝原假设）\")\n",
    "\n",
    "    # # 可选：保存标准化后的数据\n",
    "    # if args.output:\n",
    "    #     df_normalized = df.copy()\n",
    "    #     df_normalized[args.column] = normalized_data\n",
    "    #     df_normalized.to_csv(args.output, index=False)\n",
    "    #     print(f\"\\n标准化数据已保存至 {args.output}\")\n",
    "\n",
    "cal(input_file=r\"D:\\Lenovo\\Desktop\\云南大学\\毕业设计\\毕设数据\\输出数据\\csv\\MGWR结果\\MGWR_2001_2.csv\",\n",
    "         column='residual')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "myenv2",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
